<?php

class ProfileController extends Controller
{
	public $defaultAction = 'profile';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;
	/**
	 * Shows a particular model.
	 */
	public function actionProfile()
	{
		$model = $this->loadUser();
		$this->render('profile',array(
	    	'model'=>$model,
			'profile'=>$model->profile,
		));
	}


	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionEdit()
	{
		$model = $this->loadUser();
		$client= Client::getClientFromUser();
		// max_size upload
		ini_set ( "memory_limit", "10M");

		// ajax validator
		if(isset($_POST['ajax']) && $_POST['ajax']==='profile-form')
		{
			echo UActiveForm::validate(array($model,$client));
			Yii::app()->end();
		}

		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$client->attributes=$_POST['Client'];

			$file_image = CUploadedFile::getInstance($model,'urlImage');

			if ( (is_object($file_image) && get_class($file_image)==='CUploadedFile')){
				// chemin d'acces de l'image
				$model->urlImage = Yii::app()->params['ImageProfilsFolder'].'/'.$file_image;
			}

			if($model->validate()&&$client->validate()) {
				$model->save();
				$client->save();

				if (is_object($file_image)){

					$folder=Yii::app()->basePath.'/../'.Yii::app()->params['ImageProfilsFolder'];

					//Enregistrement de l' image
					$file_image->saveAs($folder.'/'.$file_image);
				}

				$this->redirect(array('/client/profil'));
			}
		}

		$this->render('edit',array(
			'model'=>$model,
			'client'=>$client,
			'listePays'=>Pays::model()->findAll(),
		));
	}

	/**
	 * Change password
	 */
	public function actionChangepassword() {
		$model = new UserChangePassword;
		if (Yii::app()->user->id) {

			// ajax validator
			if(isset($_POST['ajax']) && $_POST['ajax']==='changepassword-form')
			{
				echo UActiveForm::validate($model);
				Yii::app()->end();
			}

			if(isset($_POST['UserChangePassword'])) {
				$model->attributes=$_POST['UserChangePassword'];
				if($model->validate()) {
					$new_password = User::model()->notsafe()->findbyPk(Yii::app()->user->id);
					$new_password->password = UserModule::encrypting($model->password);
					$new_password->activkey=UserModule::encrypting(microtime().$model->password);
					$new_password->save();
					Yii::app()->user->setFlash('profileMessage',UserModule::t("New password is saved."));
					$this->redirect($this->createUrl("/client/profil"));
				}
			}
			$this->render('changepassword',array('model'=>$model));
		}
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
	 */
	public function loadUser()
	{
		if($this->_model===null)
		{
			if(Yii::app()->user->id)
			$this->_model=Yii::app()->controller->module->user();
			if($this->_model===null)
			$this->redirect(Yii::app()->controller->module->loginUrl);
		}
		return $this->_model;
	}
}